<template>
  <div class="app-container" style="width:500px;margin:auto">
    <el-form
      ref="dataForm"
      :rules="rules"
      :model="temp"
      label-position="left"
      label-width="100px"
      style="width: 400px margin-left:100px"
    >
      <el-form-item :label="$t('account.oldPassword')" prop="old_password">
        <el-input v-model="temp.old_password" type="password"></el-input>
      </el-form-item>
      <el-form-item :label="$t('account.newPassword')" prop="new_password">
        <el-input v-model="temp.new_password" type="password"></el-input>
      </el-form-item>
      <el-form-item :label="$t('account.confirmPassword')" prop="confirm_password">
        <el-input v-model="temp.confirm_password" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button native-type="reset">重置</el-button>
        <el-button type="primary" @click="onSubmit">确认修改</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
export default {
  data() {
    const confirmPassword = (rule, value, callback) => {
      if (value) {
        if (value !== this.temp.new_password) {
          callback("两次密码不一致");
        } else {
          callback();
        }
      } else {
        callback("请确认密码");
      }
    };
    return {
      regionId: "",
      temp: {
        old_password: "",
        new_password: ""
      },
      rules: {
        old_password: [
          { required: true, message: "请填写当前密码", trigger: "change" }
        ],
        new_password: [
          { required: true, message: "请填写修改密码", trigger: "change" }
        ],
        confirm_password: [
          { required: true, validator: confirmPassword, trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    resetTemp() {
      this.temp = {
        old_password: "",
        new_password: ""
      };
    },
    created() {
      this.getList();
    },
    onSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          const data = {
            old_password: this.temp.old_password,
            new_password: this.temp.new_password,
            repeat_password: this.temp.confirm_password
          };
          const editPasswordPath = {
            company: "STQ",
            api: "pc_home_editPassword",
            version: "1.0",
            data: data
          };
          this.socketApi.sendSock(JSON.stringify(editPasswordPath), res => {
            if (res) {
              if (res !== '{"type":"ping"}') {
                const resj = JSON.parse(res);
                if (resj.code === -1) {
                  this.$message.error(resj.message);
                } else {
                  this.$notify({
                    title: "成功",
                    message: "修改成功",
                    type: "success",
                    duration: 2000
                  });
                  this.resetTemp();
                  this.$nextTick(() => {
                    this.$refs["dataForm"].clearValidate();
                  });
                }
              }
            } else {
              this.$message.error(" 修改失败 ");
            }
          });
        }
      });
    }
  }
};
</script>
